/*******************************************************************************
 * This source file is default CLI main.
 *
 * @author LoveLonelyTime
 * @version 1.1
 * @date 2021-2021
 * @copyright GNU Public License.
 ******************************************************************************/

#include <iostream>
#include <fstream>

#include "language.h"
#include "analyzer.h"
#include "lexer.h"
#include "builtin.h"
#include "exception.h"

using namespace std;
using namespace SBASIC;

void print_info()
{
    std::cout << "----------------------------------------------------------------------------" << endl;
    std::cout << "|  .--.--.       ,---,.    ,---,       .--.--.      ,---,  ,----..         |" << endl
              << "| /  /    '.   ,'  .'  \\  '  .' \\     /  /    '. ,`--.' | /   /   \\        |" << endl
              << "||  :  /`. / ,---.' .' | /  ;    '.  |  :  /`. / |   :  :|   :     :       |" << endl
              << "||;  |  |--`  |   |  |: |:  :       \\ ;  |  |--`  :   |  '.   |  ;. /      |" << endl
              << "||  :  ;_    :   :  :  /:  |   /\\   \\|  :  ;_    |   :  |.   ; /--`        |" << endl
              << "| \\  \\    `. :   |    ; |  :  ' ;.   :\\  \\    `. '   '  ;;   | ;           |" << endl
              << "|  `----.   \\|   :     \\|  |  ;/  \\   \\`----.   \\|   |  ||   : |           |" << endl
              << "|  __ \\  \\  ||   |   . |'  :  | \\  \\ ,'__ \\  \\  |'   :  ;.   | '___        |" << endl
              << "| /  /`--'  /'   :  '; ||  |  '  '--' /  /`--'  /|   |  ''   ; : .'|       |" << endl
              << "|'--'.     / |   |  | ; |  :  :      '--'.     / '   :  |'   | '/  :       |" << endl
              << "|  `--'---'  |   :   /  |  | ,'        `--'---'  ;   |.' |   :    /        |" << endl
              << "|            |   | ,'   `--''                    '---'    \\   \\ .'         |" << endl
              << "|            `----'                                        `---`           |" << endl;
    std::cout << "|       Ver: " << VERSION << " Author: JHH / LOv3LOneLyT1Me                              |" << endl;
    std::cout << "----------------------------------------------------------------------------" << endl;
}

/**
 * Default CLI program
 */
int main(int argc, char *argv[])
{
    if (argc == 2)
    {
        char *file_name = argv[1];
        ifstream ifs(file_name);
        try
        {
            TokenReader token_reader(ifs);
            SyntaxAnalyer syntax_analyer(token_reader);
            Program *program = syntax_analyer.analyer();
            FunctionTable function_table;
            injection_builtin(&function_table);
            VariableTable variable_table(nullptr);
            program->run(function_table, &variable_table);
            syntax_analyer.delete_program(program);
        }
        catch (SBASIC::Exception &err)
        {
            std::cerr << "[" << err.m_indicator << "] [" << err.m_line_number << "] " << err.m_error << endl;
        }
    }
    else
    {
        print_info();
        std::cerr << "Need One SBASIC File." << endl;
    }
    return 0;
}
